﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LayerMesh : MonoBehaviour {

	// Use this for initialization
    MeshFilter _mf;
    MeshRenderer _mr;
	void Start () 
    {
        _mf = GetComponent<MeshFilter>();
        _mr = gameObject.GetComponent<MeshRenderer>();
	}

    int k = 0;
	// Update is called once per frame
	void Update () 
    {

        k++;
        if(k == 20)
        {
            UpdateMesh();
            k = 0;
        }
	}


    Mesh _layerMesh;
    void CreateMesh()
    {
        _layerMesh = _mf.mesh;
        Vector3[] points = new Vector3[(MainGame.Instance.Scene.SceneWidth +2)* (MainGame.Instance.Scene.SceneHeight + 2) * 4];
        for (int i = 0; i < MainGame.Instance.Scene.SceneWidth + 2; i++)
        {
            for (int j = 0; j < MainGame.Instance.Scene.SceneHeight + 2; j++)
            {
                points[4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 0] = new Vector3(i - 1 + 0.25f, j - 1 + 0.25f, 0.0f);
                points[4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 1] = new Vector3(i - 1 + 0.75f, j - 1 + 0.25f, 0.0f);
                points[4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 2] = new Vector3(i - 1 + 0.75f, j - 1 + 0.75f, 0.0f);
                points[4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 3] = new Vector3(i - 1 + 0.25f, j - 1 + 0.75f, 0.0f);
            }
        }
        _layerMesh.vertices = points;
        int count = 4;
        int[] trigles = new int[(MainGame.Instance.Scene.SceneWidth + 2) * (MainGame.Instance.Scene.SceneHeight + 2) * 6 * count];
        for (int i = 0; i < MainGame.Instance.Scene.SceneWidth ; i++)
        {
            for (int j = 0; j < MainGame.Instance.Scene.SceneHeight; j++)
            {
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 0] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 0;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 1] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 2;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 2] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 1;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 3] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 0;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 4] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 3;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 5] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 2;

                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 6] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 1;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 7] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 2;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 8] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i + 1) + 3;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 9] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 1;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 10] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i + 1) + 3;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 11] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i + 1) + 0;

                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 12] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 3;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 13] = 4 * ((j + 1) * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 0;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 14] = 4 * ((j + 1) * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 1;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 15] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 3;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 16] = 4 * ((j + 1) * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 1;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 17] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 2;

                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 18] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 2;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 19] = 4 * ((j + 1) * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 1;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 20] = 4 * ((j + 1) * (MainGame.Instance.Scene.SceneWidth + 2) + (i + 1)) + 0;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 21] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 2;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 22] = 4 * ((j + 1) * (MainGame.Instance.Scene.SceneWidth + 2) + (i + 1)) + 0;
                trigles[count * 6 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 23] = 4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i + 1) + 3;
            }
        }
        _layerMesh.SetIndices(trigles, MeshTopology.Triangles, 0);
        _mf.mesh = _layerMesh;
        _colors = new Color32[(MainGame.Instance.Scene.SceneWidth + 2) * (MainGame.Instance.Scene.SceneHeight + 2) * 4];
        UpdateValue();
        _layerMesh.UploadMeshData(false);
//        UnityEditor.AssetDatabase.CreateAsset(_layerMesh, "Assets/a.asset");
    }
    Color32[] _colors;

    static DataType currentDataType = DataType.None;

    public static void SetDataytype(DataType dt)
    {
        currentDataType = dt;
    }

    public enum DataType
    {
        Temperature,
        GasPressure,
        O2,
        PM10,
        None
    }

    //更新最外层的layer
    public void UpdateMesh()
    {
        if (_layerMesh == null)
        {
            CreateMesh();
        }
        if (currentDataType == DataType.None)
        { 
            _mr.enabled = false;
        }
        else
        {
            _mr.enabled = true;
            UpdateValue();
            _layerMesh.UploadMeshData(false);
        }
    }

    public void UpdateValue()
    {
        for (int i = 0; i < MainGame.Instance.Scene.SceneWidth + 2; i++)
        {
            for (int j = 0; j < MainGame.Instance.Scene.SceneHeight + 2; j++)
            {
                int ii = Mathf.Min(MainGame.Instance.Scene.SceneWidth - 1, Mathf.Max(0, i - 1));
                int jj = Mathf.Min(MainGame.Instance.Scene.SceneHeight - 1, Mathf.Max(0, j - 1));
                float v = MainGame.Instance.Scene.GetDataMyType(ii, jj, currentDataType);
                Color thecolor = GetColor(v, currentDataType);
                thecolor.a = 0.3f;
                _colors[4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 0] = thecolor;
                _colors[4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 1] = thecolor;
                _colors[4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 2] = thecolor;
                _colors[4 * (j * (MainGame.Instance.Scene.SceneWidth + 2) + i) + 3] = thecolor;
            }
        }
        _layerMesh.colors32 = _colors;
    }

    Color GetColor(float value,DataType currentDataType)
    {
        switch(currentDataType)
        {
            case DataType.O2:
                return GetColorLerp(value, Color.red, Color.yellow, Color.blue);
            case DataType.Temperature:
                return GetColorLerp(value,Color.blue, Color.yellow, Color.red);
            case DataType.GasPressure:
                return GetColorLerp(value, Color.black, Color.blue, Color.red);
            case DataType.PM10:
                return GetColorLerp(value, Color.blue, Color.yellow, Color.red);
        }
        return new Color(0.0f, 0.0f, 0.0f, 0.0f);
    }

    Color GetColorLerp(float v,Color color0,Color colorHalf,Color colorOne)
    {
        if(v < 0.5f)
        {
            return Color.Lerp(color0, colorHalf, 2.0f * v);
        }
        else 
        {
            return Color.Lerp(colorHalf, colorOne, 2.0f * v - 1.0f);
        }
    }
}
